import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Board {
    public List<List<Block>> board;

    // 生成新block
    public boolean generateNewBlock() {
        List<Block> emptyBlocks = new ArrayList<Block>();
        for (int i = 0; i < Config.row; i++) {
            for (int j = 0; j < Config.column; j++) {
                if (board.get(i).get(j).getNum() == 0) {
                    emptyBlocks.add(board.get(i).get(j));
                }
            }
        }
        int emptyPos = emptyBlocks.size();
        if (emptyPos == 0) {
            return false;
        } else {
            Random random = new Random();
            int bl = Math.abs(random.nextInt()) % emptyPos;
            Block theBlock = emptyBlocks.get(bl);
            board.get(theBlock.getR()).get(theBlock.getC()).setNum(((random.nextInt() % 2) + 1) * 2);
            return true;
        }
    }

    // 显示board
    public void showBoard() {
        for (int i = 0; i < Config.row; i++) {
            List<Block> row = this.board.get(i);
            for(int j=0;j<Config.column;j++) {
                System.out.print(row.get(j).getNum());
                System.out.print(' ');
            }
            System.out.println("");
        }
    }

    // 初始化board
    public void initBoard() {
        this.board = new ArrayList<List<Block>>();
        for (int i = 0; i < Config.row; i++) {
            List<Block> row = new ArrayList<Block>();
            for (int j = 0; j < Config.column; j++) {
                row.add(new Block(0, i, j));
            }
            this.board.add(row);
        }
    }
}
